﻿using System;
using ExFlash.Records;

namespace ExFlash.Actions
{
    public class ActionDefineFunction2 : ActionRecord
    {
        private readonly int _swfVersion;

        public StringRecord FunctionName
        {
            get;
            protected set;
        }

        public ushort NumParams
        {
            get;
            protected set;
        }

        public Byte RegisterCount
        {
            get;
            protected set;
        }

        public Boolean PreloadParentFlag
        {
            get;
            protected set;
        }

        public Boolean PreloadRootFlag 
        {
            get;
            protected set;
        }

        public Boolean SuppressSuperFlag
        {
            get;
            protected set;
        }

        public Boolean PreloadSuperFlag
        {
            get;
            protected set;
        }

        public Boolean SuppressArgumentsFlag
        {
            get;
            protected set;
        }

        public Boolean PreloadArgumentsFlag
        {
            get;
            protected set;
        }

        public Boolean SuppressThisFlag
        {
            get;
            protected set;
        }

        public Boolean PreloadThisFlag
        {
            get;
            protected set;
        }

        public Boolean PreloadGlobalFlag
        {
            get;
            protected set;
        }

        public RegisterParam[] Parameters
        {
            get;
            protected set;
        }

        public ushort CodeSize
        {
            get;
            protected set;
        }

        public ActionDefineFunction2(ushort len, int version)
            : base((byte)ActionCodes.ActionDefineFunction2, len)
        {
            _swfVersion = version;
        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            FunctionName = new StringRecord(_swfVersion);
            FunctionName.SwfDeserialize(reader);

            NumParams = reader.ReadUInt16();

            RegisterCount = reader.ReadByte();

            PreloadParentFlag = reader.ReadBoolean();
            PreloadRootFlag = reader.ReadBoolean();
            SuppressSuperFlag = reader.ReadBoolean();
            PreloadSuperFlag = reader.ReadBoolean();
            SuppressArgumentsFlag = reader.ReadBoolean();
            PreloadArgumentsFlag = reader.ReadBoolean();
            SuppressThisFlag = reader.ReadBoolean();
            PreloadThisFlag = reader.ReadBoolean();
            reader.SkipBits(7);
            PreloadGlobalFlag = reader.ReadBoolean();

            reader.AlignBits();

            Parameters = new RegisterParam[NumParams];

            for (int i = 0; i < NumParams; i++)
            {
                Parameters[i] = new RegisterParam(_swfVersion);
                Parameters[i].SwfDeserialize(reader);
            }

            CodeSize = reader.ReadUInt16();
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }
    }
}
